/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.agreement.web.supplychainserviceagreement;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.agreement.dao.supplychainserviceagreement.SupplyChainServiceAgreementDao;
import com.jeesite.modules.agreement.entity.supplychainserviceagreement.SupplyChainServiceAgreement;
import com.jeesite.modules.agreement.service.supplychainserviceagreement.SupplyChainServiceAgreementService;
import com.jeesite.modules.base.entity.customer.Customer;
import com.jeesite.modules.base.entity.guarantor.Guarantor;
import com.jeesite.modules.base.service.customer.CustomerService;
import com.jeesite.modules.base.service.guarantor.GuarantorService;
import com.jeesite.modules.sys.entity.Company;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.CompanyService;
import com.jeesite.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应链服务协议Controller
 * @author Kilo
 * @version 2021-03-03
 */
@Controller
@RequestMapping(value = "${adminPath}/agreement/supplychainserviceagreement/supplyChainServiceAgreement")
public class SupplyChainServiceAgreementController extends BaseController {

	@Autowired
	private SupplyChainServiceAgreementService supplyChainServiceAgreementService;
	@Autowired
	private SupplyChainServiceAgreementDao supplyChainServiceAgreementDao;

	@Autowired
	private GuarantorService guarantorService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private CustomerService customerService;
	@Autowired
	private UserService userService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public SupplyChainServiceAgreement get(String supplyChainServiceAgreementId, boolean isNewRecord) {
		return supplyChainServiceAgreementService.get(supplyChainServiceAgreementId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("agreement:supplyChainServiceAgreement:view")
	@RequestMapping(value = {"list", ""})
	public String list(SupplyChainServiceAgreement supplyChainServiceAgreement, Model model) {
		model.addAttribute("supplyChainServiceAgreement", supplyChainServiceAgreement);
		List<Guarantor> guarantorList = guarantorService.findList(new Guarantor());
		model.addAttribute("guarantorList", guarantorList);
		return "modules/agreement/supplychainserviceagreement/supplyChainServiceAgreementList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("agreement:supplyChainServiceAgreement:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SupplyChainServiceAgreement> listData(SupplyChainServiceAgreement supplyChainServiceAgreement, HttpServletRequest request, HttpServletResponse response) {
		supplyChainServiceAgreement.setPage(new Page<>(request, response));
		Page<SupplyChainServiceAgreement> page = supplyChainServiceAgreementService.findPage(supplyChainServiceAgreement);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("agreement:supplyChainServiceAgreement:view")
	@RequestMapping(value = "form")
	public String form(SupplyChainServiceAgreement supplyChainServiceAgreement, Model model) {
		if(supplyChainServiceAgreement.getIsNewRecord()){
			Company company = new Company();
			company.setCompanyCode("COM00000001");
			Company com = companyService.get(company);
			supplyChainServiceAgreement.setCompanyAddress(com.getCompanyAddress());
			supplyChainServiceAgreement.setCompanyContactPerson(com.getContactPerson());
			supplyChainServiceAgreement.setCompanyContactPersonAddr(com.getContactPersonAddr());
			supplyChainServiceAgreement.setCompanyEmail(com.getEmail());
			supplyChainServiceAgreement.setCompanyPhone(com.getPhone());
			supplyChainServiceAgreement.setCompany(com);
		}
		model.addAttribute("listUser", userService.findList(new User()));
		model.addAttribute("supplyChainServiceAgreement", supplyChainServiceAgreement);
		List<Guarantor> guarantorList = guarantorService.findList(new Guarantor());
		model.addAttribute("guarantorList", guarantorList);
		return "modules/agreement/supplychainserviceagreement/supplyChainServiceAgreementForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("agreement:supplyChainServiceAgreement:view")
	@RequestMapping(value = "detail")
	public String detail(SupplyChainServiceAgreement supplyChainServiceAgreement, Model model) {
		model.addAttribute("supplyChainServiceAgreement", supplyChainServiceAgreement);
		model.addAttribute("listUser", userService.findList(new User()));
		List<Guarantor> guarantorList = guarantorService.findList(new Guarantor());
		model.addAttribute("guarantorList", guarantorList);
		return "modules/agreement/supplychainserviceagreement/supplyChainServiceAgreementDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("agreement:supplyChainServiceAgreement:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated SupplyChainServiceAgreement supplyChainServiceAgreement) {
		//判断是否有交叉日期

		SupplyChainServiceAgreement checkModel=new SupplyChainServiceAgreement();
		checkModel.setCustomer(supplyChainServiceAgreement.getCustomer());
		checkModel.setCompany(supplyChainServiceAgreement.getCompany());
		checkModel.setStartDate(supplyChainServiceAgreement.getStartDate());
		checkModel.setEndDate(supplyChainServiceAgreement.getEndDate());
		checkModel.setSupplyChainServiceAgreementId(supplyChainServiceAgreement.getSupplyChainServiceAgreementId());
		List<SupplyChainServiceAgreement> list=supplyChainServiceAgreementDao.checkMore(checkModel);
		if(list.size()>0){
			return renderResult(Global.FALSE,text("已存在交叉日期的供应链服务协议，请核实！"));
		}
		supplyChainServiceAgreementService.save(supplyChainServiceAgreement);
		return renderResult(Global.TRUE, text("保存供应链服务协议成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("agreement:supplyChainServiceAgreement:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(SupplyChainServiceAgreement supplyChainServiceAgreement) {
		supplyChainServiceAgreementService.delete(supplyChainServiceAgreement);
		return renderResult(Global.TRUE, text("删除供应链服务协议成功！"));
	}

	/**
	 * 获取公司相关信息
	 * @param companyId
	 */
	@RequestMapping(value = "getCompanyInfo")
	@ResponseBody
	public Company getCompanyInfo(@RequestParam("companyId") String companyId) {
		Company company = new Company();
		company.setCompanyCode(companyId);
		Company result = companyService.get(company);
		return result;
	}

	/**
	 * 获取客户相关信息
	 * @param customerId
	 */
	@RequestMapping(value = "getCustomerInfo")
	@ResponseBody
	public Customer getCustomerInfo(@RequestParam("customerId") String customerId) {
		Customer customer = new Customer();
		customer.setCustomerId(customerId);
		Customer result = customerService.get(customer);
		return result;
	}

	/**
	 * 获取担保人相关信息
	 * @param guarantorId
	 */
	@RequestMapping(value = "getGuarantorInfo")
	@ResponseBody
	public Guarantor getGuarantorInfo(@RequestParam("guarantorId") String guarantorId) {
		Guarantor guarantor = new Guarantor();
		guarantor.setGuarantorId(guarantorId);
		Guarantor result = guarantorService.get(guarantorId);
		return result;
	}
}